package com.example.demo.controller;

import com.example.demo.dto.UserDto;
import com.example.demo.server.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 1. 查询用户钱包余额
     * @param id 用户id
     * @return
     */
    @GetMapping("queryBalance/{id}")
    public Double queryBalance(@PathVariable("id") Integer id){
        // 调用自定义方法查询余额
        Double balance = userService.queryBalance(id);
        return balance;
    }


    /**
     * 根据用户id去减少账户消费的金额100
     * @param id 用户id
     * @param money 消费金额
     * @return
     */
    @GetMapping("consume/{id}/{money}")
    public boolean consume(@PathVariable Integer id,
                          @PathVariable Double money){
        // 根据用户id去消费余额
        boolean flag = userService.consume(id, money);

        return flag;
    }


    /**
     * 根据用户id去增加账户金额20
     * @param id
     * @param money
     * @return
     */
    @GetMapping("refund/{id}/{money}")
    public boolean refund(@PathVariable Integer id,
                           @PathVariable Double money){
        // 根据用户id去消费余额
        boolean flag = userService.refund(id, money);

        return flag;
    }


    /**
     * 查询用户钱包金额变动明细的接口
     * @param id 查询用户id
     * @return  返回的明细
     */
    @GetMapping("query/{id}")
    public List<UserDto> queryAllMinuteById(@PathVariable("id") Integer id){
        // 根据用户id查询所有的明细
        List<UserDto> userDtos = userService.queryAllMinute(id);

        return userDtos;
    }
}
